package com.zdx.yswl.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.Text;
import com.umeng.analytics.MobclickAgent;
import com.zdx.yswl.R;
import com.zdx.yswl.http.KAsyncTask.TaskDelegate;
import com.zdx.yswl.ui.adapter.NewsAdapter;
import com.zdx.yswl.ui.base.BaseActivity;
import com.zdx.yswl.ui.bean.NewsBean;
import com.zdx.yswl.ui.exception.AppException;
import com.zdx.yswl.ui.exception.NetworkException;
import com.zdx.yswl.ui.exception.NoDataException;
import com.zdx.yswl.ui.task.NewsTask;
import com.zdx.yswl.view.dialog.ConfirmDialog;
import com.zdx.yswl.view.dialog.ConfirmDialog.ConfirmDialogDelegate;
import com.zdx.yswl.view.listview.BaseListView;
/**
 * 最新动态
 * @author yunshuwanli
 *
 */

public class NewsActivity extends BaseActivity implements TaskDelegate {

	protected static final String TAG = NewsActivity.class.getSimpleName();
	
	TextView  msg;
	NewsAdapter adapter;
	BaseListView listView;
	
	private String requestId;
	public static Intent actionStart(Context context, String requestId) {
		Intent intent = new Intent(context, NewsActivity.class);
		intent.putExtra("projectId", requestId);
		return intent;
	}

	@Override
	protected void initSettings()
	{

	}

	@Override
	protected void initUi()
	{

	}

	@Override
	protected void saveSettings()
	{

	}

	@Override
	protected void freeMe()
	{

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news);
		
		requestId = getIntent().getStringExtra("projectId");
		setHeader("最新动态");
		initData();
	}

	private void initData() {
		listView  = (BaseListView) findViewById(R.id.listview);
		adapter = new NewsAdapter(this);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectId", requestId);
		NewsTask task = new NewsTask(-1, this);
		task.execute(map);
		
		listView.setVisibility(View.GONE);
		startProgress();
//		msg = (TextView) findViewById(R.id.note_msg);
	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
		closeProgress();
		if (object instanceof NoDataException) {
			Toast.makeText(this, ((NoDataException) object).getMessage(), Toast.LENGTH_SHORT).show();
		} else if (object instanceof NetworkException) {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
		} else if (object instanceof AppException) {
			
			ConfirmDialog.share(this, -1, new ConfirmDialogDelegate() {
				@Override
				public void onClick_ConfirmDialog(int __id, int __position) {
					finish();
				}
			});
			ConfirmDialog.showDialog("提示", ((AppException) object).getMessage(), "确定");
		}
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		closeProgress();
		listView.setVisibility(View.VISIBLE);
		adapter.dataList = (ArrayList<NewsBean>) object;
		adapter.notifyDataSetChanged();
		listView.setAdapter(adapter);
	}
	
	
	/**友盟session的统计*/
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	/**友盟session的统计*/
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public void onNotify(int what, int arg1, int arg2, Object obj)
	{

	}

	@Override
	public void onHandler(int what, int arg1, int arg2, Object obj)
	{

	}
}
